/**
    @author: ChenLin
    @date: 2022/11/16 todo mysql操作类
**/

package handler

import (
	"flamingo/logger"
	"flamingo/mysql"
	"fmt"
	"log"
	"time"
)

type VideoProcessMysql struct {
	mysql.MysqlBaseHandler
}

func (self *VideoProcessMysql) Ping() error {
	var err error
	if err = self.Conn.Ping(); err != nil {
		logger.Logln(logger.ERROR, "ping", err)
		return err
	}
	return err
}

func NewMysql() *VideoProcessMysql {
	return &VideoProcessMysql{}
}

func (self *VideoProcessMysql) InsertVideoProcess(videoProcess VideoProcess) (int32, error) {
	if err := self.Ping(); err != nil {
		logger.Logln(logger.ERROR, err)
		return 0, err
	}
	SQL := "insert into video_process_task_info set "
	var any []interface{}
	if videoProcess.OrderId != 0 {
		SQL = SQL + "order_id = ? ,"
		any = append(any, videoProcess.OrderId)
	}
	if videoProcess.Business != "" {
		SQL = SQL + "business = ? ,"
		any = append(any, videoProcess.Business)
	}
	if videoProcess.Url != "" {
		SQL = SQL + "url = ? ,"
		any = append(any, videoProcess.Url)
	}
	if videoProcess.RedirectUrl != "" {
		SQL = SQL + "redirect_Url = ? ,"
		any = append(any, videoProcess.RedirectUrl)
	}
	if videoProcess.Type != 0 {
		SQL = SQL + "type = ? ,"
		any = append(any, videoProcess.Type)
	}
	if videoProcess.DownloadUrl != "" {
		SQL = SQL + "download_url = ? ,"
		any = append(any, videoProcess.DownloadUrl)
	}
	if videoProcess.ScreenUrl != "" {
		SQL = SQL + "screen_url = ? ,"
		any = append(any, videoProcess.ScreenUrl)
	}
	if videoProcess.ImageUrl != "" {
		SQL = SQL + "image_url = ? ,"
		any = append(any, videoProcess.ImageUrl)
	}
	if videoProcess.CompressUrl != "" {
		SQL = SQL + "compress_url = ? ,"
		any = append(any, videoProcess.CompressUrl)
	}
	if videoProcess.VideoUrl != "" {
		SQL = SQL + "video_url = ? ,"
		any = append(any, videoProcess.VideoUrl)
	}
	if videoProcess.Status != 0 {
		SQL = SQL + "status = ? ,"
		any = append(any, videoProcess.Status)
	}
	SQL = SQL + "add_time = ?"
	any = append(any, time.Now().Unix())
	stmt, err := self.Conn.Prepare(SQL)
	log.Println("insert sql :", SQL, fmt.Sprintf("%+v", any...))
	defer stmt.Close()
	if err != nil {
		log.Println("insert err:", err, SQL)
		return 0, err
	}
	res, err := stmt.Exec(any...)
	if err != nil {
		log.Println("insert err:", err, SQL)
		return 0, err
	}
	LastInsertId, err := res.LastInsertId()
	return int32(LastInsertId), nil
}

func (self *VideoProcessMysql) UpdateVideoProcess(videoProcess VideoProcess) error {
	if err := self.Ping(); err != nil {
		logger.Logln(logger.ERROR, err)
		return err
	}
	SQL := "update video_process_task_info set "
	var any []interface{}
	if videoProcess.OrderId != 0 {
		SQL = SQL + "order_id = ? ,"
		any = append(any, videoProcess.OrderId)
	}
	if videoProcess.Business != "" {
		SQL = SQL + "business = ? ,"
		any = append(any, videoProcess.Business)
	}
	if videoProcess.Url != "" {
		SQL = SQL + "url = ? ,"
		any = append(any, videoProcess.Url)
	}
	if videoProcess.RedirectUrl != "" {
		SQL = SQL + "redirect_Url = ? ,"
		any = append(any, videoProcess.RedirectUrl)
	}
	if videoProcess.Type != 0 {
		SQL = SQL + "type = ? ,"
		any = append(any, videoProcess.Type)
	}
	if videoProcess.DownloadUrl != "" {
		SQL = SQL + "download_url = ? ,"
		any = append(any, videoProcess.DownloadUrl)
	}
	if videoProcess.ScreenUrl != "" {
		SQL = SQL + "screen_url = ? ,"
		any = append(any, videoProcess.ScreenUrl)
	}
	if videoProcess.ImageUrl != "" {
		SQL = SQL + "image_url = ? ,"
		any = append(any, videoProcess.ImageUrl)
	}
	if videoProcess.CompressUrl != "" {
		SQL = SQL + "compress_url = ? ,"
		any = append(any, videoProcess.CompressUrl)
	}
	if videoProcess.VideoUrl != "" {
		SQL = SQL + "video_url = ? ,"
		any = append(any, videoProcess.VideoUrl)
	}
	if videoProcess.Status != 0 {
		SQL = SQL + "status = ? ,"
		any = append(any, videoProcess.Status)
	}
	SQL = SQL[0 : len(SQL)-1]
	if videoProcess.TaskId != 0 {
		SQL = SQL + "where task_id = ? "
		any = append(any, videoProcess.TaskId)
	}
	stmt, err := self.Conn.Prepare(SQL)
	log.Println("update sql :", SQL, fmt.Sprintf("%+v", any...))
	defer stmt.Close()
	if err != nil {
		log.Println("update err:", err, SQL)
		return err
	}
	query, err := stmt.Query(any...)
	defer query.Close()
	if err != nil {
		log.Println("update err:", err, SQL)
		return err
	}
	return nil
}

func (self *VideoProcessMysql) GetVideoProcess(videoProcess VideoProcess) (*[]VideoProcess, error) {
	var result = &[]VideoProcess{}
	var process = []VideoProcess{}
	if err := self.Ping(); err != nil {
		logger.Logln(logger.ERROR, err)
		return result, err
	}
	SQL := "select * from video_process_task_info where 1 = 1 "
	var any []interface{}
	if videoProcess.OrderId != 0 {
		SQL = SQL + "and order_id = ?"
		any = append(any, videoProcess.OrderId)
	}
	if videoProcess.Business != "" {
		SQL = SQL + "and business = ?"
		any = append(any, videoProcess.Business)
	}
	if videoProcess.Url != "" {
		SQL = SQL + "and url = ?"
		any = append(any, videoProcess.Url)
	}
	if videoProcess.RedirectUrl != "" {
		SQL = SQL + "and redirect_Url = ?"
		any = append(any, videoProcess.RedirectUrl)
	}
	if videoProcess.Type != 0 {
		SQL = SQL + "and type = ?"
		any = append(any, videoProcess.Type)
	}
	if videoProcess.DownloadUrl != "" {
		SQL = SQL + "and download_url = ?"
		any = append(any, videoProcess.DownloadUrl)
	}
	if videoProcess.ScreenUrl != "" {
		SQL = SQL + "and screen_url = ?"
		any = append(any, videoProcess.ScreenUrl)
	}
	if videoProcess.ImageUrl != "" {
		SQL = SQL + "and image_url = ?"
		any = append(any, videoProcess.ImageUrl)
	}
	if videoProcess.CompressUrl != "" {
		SQL = SQL + "and compress_url = ?"
		any = append(any, videoProcess.CompressUrl)
	}
	if videoProcess.VideoUrl != "" {
		SQL = SQL + "and video_url = ?"
		any = append(any, videoProcess.VideoUrl)
	}
	if videoProcess.Status != 0 {
		SQL = SQL + "and status = ?"
		any = append(any, videoProcess.Status)
	}
	if videoProcess.TaskId != 0 {
		SQL = SQL + "and task_id = ?"
		any = append(any, videoProcess.TaskId)
	}
	stmt, err := self.Conn.Prepare(SQL)
	defer stmt.Close()
	if err != nil {
		log.Println("select err:", err, SQL)
		return result, err
	}
	query, err := stmt.Query(any...)
	defer query.Close()

	for query.Next() {
		var tmpProcess = VideoProcess{0, 0, "", "", "", 0, "", "", "", "", "", 0}
		query.Scan(&tmpProcess.TaskId, &tmpProcess.OrderId, &tmpProcess.Business, &tmpProcess.Url, &tmpProcess.RedirectUrl, &tmpProcess.Type, &tmpProcess.DownloadUrl, &tmpProcess.ScreenUrl, &tmpProcess.ImageUrl, &tmpProcess.CompressUrl, &tmpProcess.VideoUrl, &tmpProcess.Status)
		process = append(process, tmpProcess)
	}
	if err != nil {
		log.Println("select err:", err, SQL)
		return result, err
	}
	result = &process
	return result, nil
}
